#include "stdafx.h"

#include "ProviderTbl.h"

CProviderTbl::CProviderTbl()
{
	m_sName = _T("nha_cung_cap");
}
CProvider* CProviderTbl::getProvider(CString sIDProvider)
{
	CProvider *pProvider = NULL;
	MYSQL_RES * pResult = NULL;
	CString sNewQuery;
	mysql_query(conn,"SET character_set_results=utf8");
	sNewQuery.Format(_T("SELECT * FROM %s.%s where ma_nha_cung_cap = '%s'"),g_sDatabase,TABLE_NAME_PROVIDER,sIDProvider);
	CT2A ascii(sNewQuery, CP_UTF8);		
	mysql_query(conn,ascii.m_psz);
	pResult = mysql_use_result(conn);
	if (pResult)
	{
		MYSQL_ROW row = mysql_fetch_row(pResult);
		if (row)
		{
			pProvider = new CProvider;
			pProvider->setId(sIDProvider);
			pProvider->setName(CString (CA2W(row[1], CP_UTF8)));//name	
			pProvider->setAdress(CString (CA2W(row[2], CP_UTF8)));//Addrress
			pProvider->setMobile(CString(CA2W(row[3], CP_UTF8)));//mobile
			pProvider->setFax(CString(CA2W(row[4], CP_UTF8)));//fax
			pProvider->setNote(CString(CA2W(row[5], CP_UTF8)));//note
		}
		mysql_free_result(pResult);
	}
	return pProvider;
}

 vector<CProvider*> CProviderTbl::getListProvider()
{
	vector<CProvider*> vtProvider;

	MYSQL_RES * pResult = NULL;
	CString sNewQuery;
	mysql_query(conn,"SET character_set_results=utf8");
	
	sNewQuery.Format(_T("SELECT * FROM %s.%s "),g_sDatabase,TABLE_NAME_PROVIDER);
	CT2A ascii(sNewQuery, CP_UTF8);		
	mysql_query(conn,ascii.m_psz);
	pResult = mysql_use_result(conn);
	if (pResult)
	{
		MYSQL_ROW row = NULL;
		while((row = mysql_fetch_row(pResult)) )
		{
			CProvider * pProvider = new CProvider;
			pProvider->setId(CString (CA2W(row[0], CP_UTF8)));
			pProvider->setName(CString (CA2W(row[1], CP_UTF8)));//name	
			pProvider->setAdress(CString (CA2W(row[2], CP_UTF8)));//Addrress
			pProvider->setMobile(CString(CA2W(row[3], CP_UTF8)));//mobile
			pProvider->setFax(CString(CA2W(row[4], CP_UTF8)));//fax
			pProvider->setNote(CString(CA2W(row[5], CP_UTF8)));//note
			vtProvider.push_back(pProvider);
		}
		mysql_free_result(pResult);
	}
	return vtProvider;
}

 bool CProviderTbl::insertProvider(CProvider* pProvider)
 {
	if (pProvider)
	{
		CString sValue;
		CSqlTable::m_sName = TABLE_NAME_PROVIDER;
		sValue.Format(_T("('%s',_utf8'%s', _utf8'%s','%s', '%s',_utf8'%s')")
			,pProvider->getId(),pProvider->getName(),pProvider->getAdress(),pProvider->getMobile(),pProvider->getFax(),pProvider->getNote());

		return insertRecord(TBL_PROVIDER,sValue);
	}
	return false;
 }

 int CProviderTbl::getMaxCode()
 {
	 int nMax = 0;
	 MYSQL_RES * pResult = NULL;

	 CString sNewQuery;
	 mysql_query(conn,"SET character_set_results=utf8");
	 sNewQuery.Format(_T("SELECT %s ,MAX(%s)FROM %s.%s "),FIELD_CODE_PROVIDER,FIELD_CODE_PROVIDER,g_sDatabase,TABLE_NAME_PROVIDER);
	 CT2A ascii(sNewQuery, CP_UTF8);		
	 mysql_query(conn,ascii.m_psz);
	 pResult = mysql_use_result(conn);
	 if (pResult)
	 {
		 MYSQL_ROW row = mysql_fetch_row(pResult);
		 if (row)
		 {
			 CString sTemplate;
			 sTemplate = row[1];
			 sTemplate.Replace(_T("NCC"),_T("0"));
			 nMax = _tstoi(sTemplate);
		 }
		 mysql_free_result(pResult);
	 }
	 return nMax;
 }